<?php

require_once 'PipesImpl/Database/Reflection.php';
require_once 'PipesImpl/Database/Reflection/Table.php';
require_once 'Pipes/Database/Reflection/Column.php';

class Pipes_Database_Reflection_Table implements PipesImpl_Database_Reflection_Table
{
    private $_pdo = null;
    private $_parent = null;
    private $_name = '';
    
    public function __construct(PipesImpl_Database_Reflection $parent, $name)
    {
        $this->_pdo = $parent->getPDO();
        $this->_parent = $parent;
        $this->_name = $name;
    }
    
    public function getPDO()
    {
        return $this->_pdo;
    }
    
    public function getName()
    {
        return $this->_name;
    }
    
    public function getColumns() 
    { 
        $query = 'DESCRIBE ' . $this->getName();
        $statement = $this->_pdo->query($query);
        
        $returnArray = array();
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        $rowset = $statement->fetchAll();
        $statement->closeCursor();
        foreach ($rowset as $row) {
            $column = new Pipes_Database_Reflection_Column($this, $row['Field']);
            $returnArray[$row['Field']] = $column;
        }
        
        return $returnArray;
    }
    
    public function getColumn($name) 
    { 
        return new Pipes_Database_Reflection_Column($this, $name);
    }
}
